package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.system.SystemProvinceBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemProvinceDao;
import com.cw.jeeyt.dao.model.system.SystemProvinceDo;
import com.cw.jeeyt.service.converter.system.SystemProvinceConverter;
import com.cw.jeeyt.service.intf.system.SystemProvinceService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 省份信息业务实现类
 *
 * @author chenw
* @version 1.0.0
 */
@Service
public class SystemProvinceServiceImpl implements SystemProvinceService {

    private static final Logger logger = LoggerFactory.getLogger(SystemProvinceServiceImpl.class);
    @Autowired
    private SystemProvinceDao systemProvinceDao;

    /**
     * 新增SystemProvince
     *
     * @param systemProvinceBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemProvince(SystemProvinceBo systemProvinceBo) throws Exception {

        if (systemProvinceBo == null) {
            logger.error("省份信息为空");
            return;
        }
        SystemProvinceDo systemProvince = SystemProvinceConverter.bo2Do(systemProvinceBo);
        systemProvince.setId(SequenceTool.nextId());
        systemProvince.setCreateBy(SessionUtil.getSessionUserId());
        systemProvince.setCreateDate(new Date());
        systemProvince.setUpdateBy(SessionUtil.getSessionUserId());
        systemProvince.setUpdateDate(new Date());
        systemProvinceDao.insertSystemProvince(systemProvince);
    }

    /**
     * 更新SystemProvince信息
     *
     * @param systemProvinceBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystemProvince(SystemProvinceBo systemProvinceBo) throws Exception {

        if (systemProvinceBo == null) {
            logger.error("省份信息为空");
            return;
        }
        SystemProvinceDo systemProvince = SystemProvinceConverter.bo2Do(systemProvinceBo);
        systemProvince.setUpdateBy(SessionUtil.getSessionUserId());
        systemProvince.setUpdateDate(new Date());
        systemProvinceDao.updateSystemProvince(systemProvince);
    }

    /**
     * 删除SystemProvince
     *
     * @param systemProvinceBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSystemProvince(SystemProvinceBo systemProvinceBo) throws Exception {
        if (systemProvinceBo == null) {
            logger.error("省份信息为空");
            return;
        }
        SystemProvinceDo systemProvince = SystemProvinceConverter.bo2Do(systemProvinceBo);
        systemProvinceDao.deleteSystemProvince(systemProvince);

    }

    /**
     * 分页查询SystemProvince
     *
     * @param systemProvinceBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemProvinceBo> queryByPage(SystemProvinceBo systemProvinceBo, Integer startNum, Integer pageSize)
    throws Exception {
        PageInfo<SystemProvinceDo> pageInfo = systemProvinceDao.queryByPage(
            SystemProvinceConverter.bo2Do(systemProvinceBo), startNum, pageSize);
        return new PageInfo<>(pageInfo, SystemProvinceConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据国家编码查询省份信息
     * countryCode 国家编码
     */
    @Override
    public List<SystemProvinceBo> loadProvinceByCountryCode(String countryCode) throws Exception {
        return SystemProvinceConverter.doList2BoList(systemProvinceDao.loadProvinceByCountryCode(countryCode));
    }

    /**
     * 根据主键查询SystemProvince信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemProvinceBo getSystemProvinceById(String id) throws Exception {
        return SystemProvinceConverter.do2Bo(systemProvinceDao.getSystemProvinceById(id));
    }
}
